﻿


using Controller;
using HBase;
using HBase.db;
using hCommand;
using Net.http;
using Persistence;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace WebApi {
    [FormAuthenticationFilter]

    [RoutePrefix("pk")]
    public class PkController : BaseApiController {
        IDoPkServer pkServer = WMSDalFactray.getDal<IDoPkServer>();
        ICoreStockServer coreStockServer = WMSDalFactray.getDal<ICoreStockServer>();
        [HttpPost, Route("findPage")]
        public BaseResult FindPage([FromBody] PkDto dto) {
            Page<WhDopk> info = pkServer.queryPkPage(dto);
            return BaseResult.Ok(info);
        }

        [HttpPost, Route("FindCoreStockPage")]
        public BaseResult FindCoreStockPage([FromBody] CoreStockDto dto) {
            Page<CoreStock> info = pkServer.queryCoreStockPage(dto);
            return BaseResult.Ok(info);
        }

        [HttpPost, Route("hisPage"), ControlName("盘库明细")]
        public BaseResult hisPage([FromBody] PkDto dto) {
            List<WhDopkRes> l = pkServer.hisPage(dto);

            return BaseResult.Ok(l);
        }
        [HttpPost, Route("getPk")]
        public BaseResult getPk([FromBody] WhDopk pk) {
            WhDopk info = pkServer.getById<WhDopk>(pk.id);
            return BaseResult.Ok(info);
        }


        //物料保存
        [HttpPost, Route("getDetials")]
        public BaseResult getDetials([FromBody] WhDoi dto) {
            List<WhDopkDet> info = pkServer.getDetials(dto.id);
            return BaseResult.Ok(info);
        }

        [HttpPost, Route("save"), ControlName("保存盘库单及明显")]
        public BaseResult savePk([FromBody] DopkDto dto) {
            WhDopk doo = dto.main;
            if (doo.id == 0) {
                doo.status = 1;
            } else {
                WhDopk pk = pkServer.getById<WhDopk>(doo.id);
                if (pk.status != 1) {
                    return BaseResult.Error("出库单非新建待执行状态无法修改!");
                }
            }
            WMSTransactionFacade.doOne2ManyEngine(dto);
            return BaseResult.Ok(dto);
        }
        [HttpPost, Route("printPk")]
        public BaseResult printDetail([FromBody] CoreStockDetalDto dto) {
            PrintDetail pd = new PrintDetail();
            pd.info = pkServer.coreStockDetailList(dto.pkId);
            pd.html = getPrintUtf8("pk.html");
            return BaseResult.Ok(pd);
        }
        [HttpPost, Route("printPkDiff")]
        public BaseResult printPkDiff([FromBody] CoreStockDetalDto dto) {
            PrintDetail pd = new PrintDetail();
            List<CoreStockDet> oldList = pkServer.listPkOldDiffDetail(dto.pkId);
            pd.info = oldList;
            pd.html = getPrintUtf8("pkDiff.html");

            return BaseResult.Ok(pd);
        }
        //物料保存
        [HttpPost, Route("delete"), ControlName("盘库保存")]
        public BaseResult delete([FromBody] WhDopk pk) {
            pk.status = -1;
            pkServer.updateNotNull(pk);
            return BaseResult.Ok("删除成功");
        }


        [HttpPost, Route("selectStock/{pkId}/{add}/{del}"), ControlName("修改盘库托盘")]
        public BaseResult selectStock(long pkId, String add, String del) {
            WMSTransactionFacade.doPkStockEngine(pkId, add, del);
            return BaseResult.Ok("删除成功");
        }
        [HttpPost, Route("pkLess"), ControlName("盘库剩余")]
        public BaseResult pkLess([FromBody] CoreStockDet t) {
            double remain = t.countDb;
            CoreStockDet old = pkServer.getById<CoreStockDet>(t.id);
            CoreStock cs = pkServer.getById<CoreStock>(old.pid);
            WhDopk pk = pkServer.getById<WhDopk>(cs.pkId);
            if (pk == null || pk.status != (int)DoStatus.执行中) {
                return BaseResult.Ok("盘库单状态异常");
            }

            if (remain == old.countDb) {
                return BaseResult.Ok("无变化");
            }
            if (remain == 0) {
                old.status = (int)StockDetailStatus.盘亏中;
                pkServer.update(old);
                return BaseResult.Ok("全部盘亏");
            }
            CoreStockDet now = (CoreStockDet)old.Clone();
            now.id = 0;
            if (remain < old.countDb) {
                now.status = (int)StockDetailStatus.盘亏中;
                now.countDb = old.countDb - remain;
                old.countDb = remain;
                pkServer.update(old);
            } else if (remain > old.countDb) {
                now.status = (int)StockDetailStatus.盘盈;
                now.countDb = remain - old.countDb;
            }
            pkServer.insert(now);
            return BaseResult.Ok("");
        }
        [HttpPost, Route("pkMore"), ControlName("pad盘盈保存")]
        public BaseResult pkMore(CoreStockDet model) {
            CoreStock cs = coreStockServer.getCoreStockByCode (model.boxCode);
            if (cs == null) {
                return BaseResult.Ok("托盘不存在");
            }
            model.pid = cs.id;
            WhDopk pk = pkServer.getById<WhDopk>(cs.pkId);
            if (pk == null || pk.status != (int)DoStatus.执行中) {
                return BaseResult.Ok("盘库单状态异常");
            }
            model.doiDetId = cs.pkId;
            model.status = (int)StockDetailStatus.盘盈;
            model.doiType = DetConst.DoiTypePy;


            var item = pkServer.getById<CoreItem>(model.itemId);
            if (item == null) {
                return BaseResult.Ok("物料不存在");
            }
            model.unit = item.unit;
            WMSTransactionFacade.doChangeCSDEngine(model);

            var v = pkServer.getById<CoreStockDet>(model.id);
            return BaseResult.Ok(v);
        }

        [HttpPost, Route("enginePk"), ControlName("盘库生成")]
        public BaseResult enginePk([FromBody] WhDopk pk) {
            String mess = WMSTransactionFacade.doPkEngine(pk.id);
            return BaseResult.Ok(mess);
        }
        [HttpPost, Route("checkDetail"), ControlName("扫描盘库")]
        public BaseResult checkDetail([FromBody] CoreStockDetalDto dto) {

            return BaseResult.Ok("盘库成功");

        }

        [HttpPost, Route("finshPk"), ControlName("盘库完成")]
        public BaseResult finshPk([FromBody] WhDopk pk) {
            var res = WMSTransactionFacade.doPkFinshEngine(pk.id);
            if (res.s) {
                return BaseResult.Ok(res.msg);
            } else {
                return BaseResult.Error(res.msg);
            }
        }
    }
}